package com.lemon.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.PageResult;
import com.lemon.domain.WarehousingEntryUp;
import com.lemon.domain.dto.AddStocktakingDto;
import com.lemon.mapper.*;
import com.lemon.service.WarehousingEntryUpService;
import com.lemon.utils.UserThreadLocal;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.domain.StocktakingInfo;
import com.lemon.service.StocktakingInfoService;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class StocktakingInfoServiceImpl extends ServiceImpl<StocktakingInfoMapper, StocktakingInfo> implements StocktakingInfoService{

    private final WarehouseMapper warehouseMapper;
    private final WarehouseAreaMapper warehouseAreaMapper;
    private final WarehouseAreaLocaMapper warehouseAreaLocaMapper;
    private final WarehousingEntryUpMapper warehousingEntryUpMapper;
    private final SkuMapper skuMapper;

    @Override
    public void confirm(StocktakingInfo stocktakingInfo) {
        StocktakingInfo selectById = baseMapper.selectById(stocktakingInfo.getId());
        selectById.setStatus(2);
        selectById.setCorrectCount(stocktakingInfo.getCorrectCount());
        selectById.setDiscrepancyCount(selectById.getBookCount()-selectById.getCorrectCount());
        baseMapper.updateById(selectById);
    }

    @Override
    public PageResult getPage(Integer page, Integer pageSize, String title,Integer status,Integer category) {
        PageHelper.startPage(page,pageSize);
        List<StocktakingInfo> stocktakingInfos = baseMapper.selectList(Wrappers.<StocktakingInfo>lambdaQuery().like(ObjUtil.isNotEmpty(title), StocktakingInfo::getTitle, title).eq(ObjUtil.isNotEmpty(status), StocktakingInfo::getStatus, status).eq(ObjUtil.isNotEmpty(category), StocktakingInfo::getCategory, category).orderByDesc(StocktakingInfo::getCreateTime));
        stocktakingInfos.forEach(stocktakingInfo -> {
            stocktakingInfo.setTargetWarehouseName(warehouseMapper.selectById(stocktakingInfo.getTargetWarehouseId()).getName());
            stocktakingInfo.setTargetWarehouseAreaName(warehouseAreaMapper.selectById(stocktakingInfo.getTargetWarehouseAreaId()).getName());
            stocktakingInfo.setTargetWarehouseLocaName(warehouseAreaLocaMapper.selectById(stocktakingInfo.getTargetWarehouseAreaLocaId()).getLocaCode());
        });
        Page list = (Page) stocktakingInfos;
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    @Transactional
    public void add(List<AddStocktakingDto> addStocktakingDtos) {
        addStocktakingDtos.forEach(addStocktakingDto -> {
            WarehousingEntryUp entryUp = warehousingEntryUpMapper.selectOne(Wrappers.<WarehousingEntryUp>lambdaQuery().eq(WarehousingEntryUp::getSkuId, addStocktakingDto.getSkuId()).eq(WarehousingEntryUp::getWarehouseAreaLocaId, addStocktakingDto.getTargetWarehouseAreaLocaId()).last("limit 1"));
            StocktakingInfo stocktakingInfo = new StocktakingInfo();
            stocktakingInfo.setTargetWarehouseId(entryUp.getWarehouseId());
            stocktakingInfo.setTargetWarehouseAreaId(entryUp.getWarehouseAreaId());
            stocktakingInfo.setTargetWarehouseAreaLocaId(entryUp.getWarehouseAreaLocaId());
            stocktakingInfo.setSkuId(entryUp.getSkuId());
            stocktakingInfo.setTitle(skuMapper.selectById(entryUp.getSkuId()).getTitle());
            stocktakingInfo.setBookCount(entryUp.getCount());
            stocktakingInfo.setCategory(addStocktakingDto.getIsCategory());
            stocktakingInfo.setStatus(1);
            stocktakingInfo.setEmpId(UserThreadLocal.get() == null ? 1 : UserThreadLocal.get().getId());
            stocktakingInfo.setCreateTime(LocalDateTime.now());
            baseMapper.insert(stocktakingInfo);
        });
    }
}
